package com.zhao.dynamicProxy.CGLIBProxy.proxy;

import com.zhao.dynamicProxy.CGLIBProxy.RealSubject.TrainStation;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ProxyFactory implements MethodInterceptor {

    public TrainStation trainStation = new TrainStation();

    public TrainStation getTrainStationProxy() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(trainStation.getClass());
        enhancer.setCallback(this);
        TrainStation proxyObject = (TrainStation) enhancer.create();
        return  proxyObject;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("CGLIB Proxy ...");
        Object invoke = method.invoke(trainStation, objects);
        return invoke;
    }
}
